/*
 * Date          Author         Note
 * 2015.04.21    舞者无罪                   版本查询
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "ver.h"

#define   VER_MH     8
#define   VER_CAM    2
#define   VER_MAC    1
#define   VER_MINUS  0
/*
 *
 */
static void CreateVerInterface(HWND hWnd)
{
    //sys btn
    CreateWindow(MH_STATIC_NAME, "", WS_CHILD | WS_VISIBLE, ID_ESC,
            CX_ROCK_TRAN_BMP - CX_ASK_BMP * 2, 10, CX_ASK_BMP, CY_ASK_BMP, hWnd,
            (int) &g_bmp[BMP_FALSE]);
    CreateWindow(MH_STATIC_NAME, "", WS_CHILD | WS_VISIBLE, ID_ESC,
            CX_ROCK_TRAN_BMP - CX_ASK_BMP * 3 - 8, 10, CX_ASK_BMP, CY_ASK_BMP,
            hWnd, (int) &g_bmp[BMP_TRUE]);
    CreateWindow(MH_STATIC_NAME, "", WS_CHILD | WS_VISIBLE, ID_ESC,
            CX_ROCK_TRAN_BMP - CX_ASK_BMP * 4 - 8 * 2, 10, CX_ASK_BMP,
            CY_ASK_BMP, hWnd, (int) &g_bmp[BMP_ASK]);
    //
    const char *sc[6] = {
        GetString(556),
        GetString(557),
        GetString(558),
        GetString(559),
        GetString(560),
        GetString(561),
    };
    int i = 0;
    HWND hTmp = -1;
    VERSION version;
    memset(&version, 0, sizeof(VERSION));
    if(GetVersionInfo(g_knit_data, &version))
    {
        printf("failed to get version\n");
    }
    char str[6][128] = {{0}};
    sprintf(str[0], "%u.%u.%u.%u", VER_MH, VER_CAM,
                VER_MAC, VER_MINUS);
    sprintf(str[1], "%u.%u-%u.%u", version.rack[0], version.rack[1],
            version.rack[2], version.rack[3]);
    sprintf(str[2], "%u.%u-%u.%u", version.board[0], version.board[1],
            version.board[2], version.board[3]);
    sprintf(str[3], "%u.%u.%x-%u.%u", (version.header[0] >> 4) & 0x0F, version.header[0] & 0x0F,
            version.header[1], version.header[2], version.header[3]);
#if 1
    GetLinuxVersion(str[4]);
#else
    sprintf(str[4], "%u.%u.%u.%u", version.driver[0], version.driver[1],
            version.driver[2], version.driver[3]);
#endif
    sprintf(str[5], "%u.%u.%u.%u", 0, 0, version.csk[0], version.csk[1]);
    //
    for(i = 0; i < 6; ++i)
    {
        hTmp = CreateWindow (MH_LABEL_NAME,
            sc[i],
            WS_CHILD | BS_LEFT | WS_VISIBLE,
            IDC_STATIC,
            20,
            60 + i * 30,
            100,
            20,
            hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_GREEN);
        EnableWindow(hTmp, FALSE);

        hTmp = CreateWindow (MH_LABEL_NAME,
            str[i],
            WS_CHILD | BS_LEFT | WS_VISIBLE,
            IDC_STATIC,
            140,
            60 + i * 30,
            100,
            20,
            hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_CYAN);
        EnableWindow(hTmp, FALSE);
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    if(nID == ID_ESC)
    {
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
}
/*
 *
 */
static int VerWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateVerInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_ROCK_TRAN]);
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
int GetCarriageVersion()
{
    VERSION version;
    ushort data = 0;
    uchar *p = (uchar *)&data;
    memset(&version, 0, sizeof(VERSION));
    GetVersionInfo(g_knit_data, &version);
    p[1] =version.header[0] ;
    p[0] =  version.header[1];

    return data;
}
/*
 *
 */
HWND CreateVerWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - CX_ROCK_TRAN_BMP) / 2;
    int y = (CY_SCREEN - CY_ROCK_TRAN_BMP) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(562);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = VerWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_ROCK_TRAN_BMP;
    CreateInfo.by = y + CY_ROCK_TRAN_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    else
    {
    }
    return hWnd;
}
